package com.qingke.Qiximybatis.business;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.ibatis.session.SqlSession;
import com.qingke.Qiximybatis.QixiApp;
import com.qingke.Qiximybatis.QixiConsole;
import com.qingke.Qiximybatis.QixiDao;
import com.qingke.Qiximybatis.command.UserCommand;
import com.qingke.Qiximybatis.mapper.InvitationMapper;
import com.qingke.Qiximybatis.mapper.MyUserMapper;
import com.qingke.Qiximybatis.sqlclass.Invitation;
import com.qingke.Qiximybatis.sqlclass.MyUser;

public class ManyPersonal10 extends UserCommand{
	public ManyPersonal10(MyUser user) {
		super(user);
	}

	@Override
	public void executeUserCommand() {
		System.out.println("欢迎进入发起多人邀请界面：");
		System.out.println("可以list 查看用户信息！");
		String s= QixiConsole.askUserInput("请输入你所要邀请人的ID(用逗号隔开)：>");
		String[] ss = s.split(",");
		ArrayList<Integer> ids = new ArrayList<>();
		int count = 0;
		for(int i = 0 ; i < ss.length ; i++){
			int id = Integer.parseInt(ss[i]);
			ids.add(id);
			count++;
		}
		System.out.println(count);
		MyUser InvUser = new MyUser();
		SqlSession session = QixiDao.mybatisSession();
		MyUserMapper userM = session.getMapper(MyUserMapper.class);
		InvitationMapper inviM = session.getMapper(InvitationMapper.class);
		Invitation InviterUser = new Invitation();
		
		String datetime = QixiConsole.askUserInput("请输入约会的日期(格式：yyyy-MM-dd HH:mm:ss):>");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate2 = null;
		try {
			myDate2 = dateFormat2.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println(myDate2);
		int id = QixiApp.getInstance().getUser().getId();
		ArrayList<String> inviName = new ArrayList<>();
		for(int i = 0; i<count; i++){
			InviterUser.setInviterUserId(id);
			InviterUser.setInviteeUserId(ids.get(i));
			InviterUser.setInviteTime(myDate2);
			inviM.insert(InviterUser);
			InvUser = userM.getOneUser(ids.get(i));
			inviName.add(InvUser.getName());
			session.commit();
		}
		System.out.println("邀请成功发出！您邀请了"+ inviName);
		
		
	}
}
